<?php


/**
*	Page callback that handles the bulk updating or creation
*	of site content. We're using the BATCH API
*/
function quail_server_websites_manual_check($node) {
	$batch = array(
		'operations' => array(
			array('quail_server_manual_batch_operation', array($node)),
		),
		'finished' => 'quail_server_websites_manual_batch_finished',
		'title' => t('Crawling website'),
		'init_message' => t('Crawling starting'),
		'progress_message' => t('Crawling site and parsing pages for accessibility.'),
		'error_message' => t('The crawl encountered an error.'),
		'file' => drupal_get_path('module', 'quail_server') .'/quail_server.batch.inc',
	);
	batch_set($batch);
	batch_process('node/'. $node->nid);
}

/**
*	Batch process that handles iterating over
*	pages, collecting links, and then updating the batch process
*	to let the user know what's going on.
*/
function quail_server_manual_batch_operation($node, &$context) {
	module_load_include('inc', 'quail_server');
	if(!$context['sandbox']['node']) {
		$context['sandbox']['node'] = $node;
		$context['sandbox']['done'] = array();
		$context['sandbox']['links'] = array($node->ac_server['start_uri']);
		$context['sandbox']['next'] = 0;
	}
	$current_url = $context['sandbox']['links'][$context['sandbox']['next']];
	$result = drupal_http_request($current_url);
	if($result->redirect_code == '200' && $result->redirect_url) {
		$result = drupal_http_request($result->redirect_url);
		$current_url = $result->redirect_url;
	}
	if($result->data && strpos(strtolower($result->data), '<html') !== false && ($result->code == '200' || $result->redirect_code == '200') && _quail_server_page_can_be_crawled($node, $current_url, $result->data)) {
		$links = quail_server_parse_links($current_url, $result->data, $context['sandbox']['node']->ac_server);
		foreach($links as $link) {
			if(!$context['sandbox']['done'][$link] && !in_array($link, $context['sandbox']['links'])) {
				$context['sandbox']['links'][] = $link;
			}
		}
		$url = parse_url($current_url);
		$html = _quail_server_get_content_from_template($result->data, $node, $current_url);
		$accessibility = quail_server_check_string($html, $current_url, $context['sandbox']['node']->ac_server['guideline_nid']);
		if($accessibility !== false) {
			//If there are accessibility reports, create a node
			$node = quail_server_get_node($current_url);
			$node->accessibility_results = array('content' => $accessibility);
			$node->website = $context['sandbox']['node']->nid;
			$node->revision = TRUE;
			$node->total_nodes = _quail_server_count_nodes($result->data);
			$node->file_path = $url['path'];
			if($url['query']) {
				$node->file_path .= '?'. $url['query'];
			}
			$node->content_hash = md5($result->data);
			$node->search_text = $result->data;
			$node->title = _quail_server_get_title($result->data, $url['path']);
			node_save($node);
		}
		$context['message'] = t('Updating page %title', array('%title' => $node->title));

	}
	$context['sandbox']['done'][$current_url] = $current_url;
	$context['sandbox']['next']++;
	$context['finished'] = count($context['sandbox']['done']) / count($context['sandbox']['links']);
}

/**
*	Batch API finished callback. Lets the user know what happened with the batch process.
*/
function quail_server_websites_manual_batch_finished() {
if ($success) {
    // Here we do something meaningful with the results.
    $message = count($results) .' processed.';
    $message .= theme('item_list', $results);
  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    if(is_array($operations) || is_object($operations)) {
	    $error_operation = reset($operations);
	    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
	}
  }
  drupal_set_message($message);
}